package org.example.pg;

import java.sql.*;
import java.util.Properties;

public class SchemaTest {
    public static void main(String[] args) {
        String jdbcUrl = "jdbc:postgresql://localhost:6433/mypostgres"; // PgBouncer URL
        Properties props = new Properties();
        props.setProperty("user", "postgres");
        props.setProperty("password", "hyh.5795");
        //props.setProperty("extra_float_digits", "0");

        String[] schemas = {"sch_1", "sch_2", "sch_3"};
        try (Connection conn = DriverManager.getConnection(jdbcUrl, props)) {
            for (String schema : schemas) {
                // Set search_path to the current schema
                try (Statement stmt = conn.createStatement()) {
                   // stmt.execute("SET extra_float_digits TO 0");
                    stmt.execute("SET search_path TO " + schema);

                    // Query the test table in the current schema
                    ResultSet rs = stmt.executeQuery("SELECT id, code, name FROM test");

                    // Print the results
                    System.out.println("Results from schema: " + schema);
                    while (rs.next()) {
                        int id = rs.getInt("id");
                        String code = rs.getString("code");
                        String name = rs.getString("name");
                        System.out.println("ID: " + id + ", Code: " + code + ", Name: " + name);
                    }
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
